package com.zytech.zspider.task.impl.http;

import com.zytech.modules.lang.Langs;
import com.zytech.modules.lang.annotations.Immutable;

import java.nio.charset.Charset;
import java.util.Collections;
import java.util.Map;

/**
 * @author siuming
 */
@Immutable
public final class HttpTaskResponse {

    private final Map<String, String> headers;
    private final String mimeType;
    private final String charset;
    private final byte[] content;
    private final Object wrapContent;

    /**
     * @param builder
     */
    HttpTaskResponse(Builder builder) {
        this.headers = Langs.nullSafe(builder.headers, Collections.<String, String>emptyMap());
        this.mimeType = Langs.nullSafe(builder.mimeType, "text/plain");
        this.charset = Langs.nullSafe(builder.charset, "UTF-8");
        this.content = Langs.nullSafe(builder.content, new byte[0]);
        this.wrapContent = builder.wrapContent;
    }

    /**
     * @return
     */
    public Map<String, String> getHeaders() {
        return Collections.unmodifiableMap(headers);
    }

    /**
     * @return
     */
    public String getMimeType() {
        return mimeType;
    }

    /**
     * @return
     */
    public boolean isTextMimeType() {
        return getMimeType().contains("text") ||
                getMimeType().contains("xml") ||
                getMimeType().contains("html") ||
                getMimeType().contains("xhtml");
    }

    /**
     * @return
     */
    public boolean isImageMimeType() {
        return getMimeType().contains("image");
    }

    /**
     * @return
     */
    public boolean isOctetMimeType() {
        return getMimeType().contains("octet");
    }

    /**
     * @return
     */
    public String getCharset() {
        return charset;
    }

    /**
     * @return
     */
    public byte[] getContent() {
        return content;
    }

    /**
     * @return
     */
    public String getContentAsString() {
        return new String(getContent(), Charset.forName(getCharset()));
    }

    /**
     * @return
     */
    public Object getWrapContent() {
        return wrapContent;
    }

    /**
     * @return
     */
    public static Builder ofBuilder() {
        return new Builder();
    }

    public static class Builder {
        Map<String, String> headers;
        String mimeType;
        String charset;
        byte[] content;
        Object wrapContent;

        Builder() {
        }

        public Builder headers(Map<String, String> headers) {
            this.headers = headers;
            return this;
        }

        public Builder mimeType(String mimeType) {
            this.mimeType = mimeType;
            return this;
        }

        public Builder charset(String charset) {
            this.charset = charset;
            return this;
        }

        public Builder content(byte[] content) {
            this.content = content;
            return this;
        }

        public Builder wrapContent(Object wrapContent) {
            this.wrapContent = wrapContent;
            return this;
        }

        public HttpTaskResponse build() {
            return new HttpTaskResponse(this);
        }
    }
}
